/*
 * Last edited: 06/07/13 by Ching-Ting 
 */
package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import Model.Conference;
import Model.Controller;
import Model.Deadline;
import Model.Paper;
import Model.Review;

/**
 * Constructs the Peer-Review tab in the program.
 * 
 * @author Ching-Ting Huang
 * @version UWTCSS 360, Spring 2013. 3-squared-software-systems.
 */
public class Reviewer extends JPanel {

	private static final long serialVersionUID = -496726008902639346L;
	private Controller controller;
	private ScrollPane scroll;
	private JLabel status;
	private JLabel deadline;
	private JButton review;

	/**
	 * Initializes fields.
	 * 
	 * @param control is the Controller in the Model package.
	 * @author Ching-Ting Huang
	 */
	public Reviewer(Controller control) {
		controller = control;
		scroll = new ScrollPane(controller, "rev");
		status = new JLabel("(Select a manuscript to see check whether the manuscript has been reviewed.");
		deadline = new JLabel("(Select a manuscript to see the decision date: YYYY/MM/DD)");
		review = new JButton("Edit/Create Review");
		setup();
	} //Review

	/**
	 * Sets up the basic look and catch selection change from JTable in JScrollPane
	 * by utilizing propertyChangeListener.
	 * 
	 * @author Ching-Ting Huang
	 */
	private void setup() {
		final JLabel flag = new JLabel("rev");

		/*
		  Using property change listener to catch list selection changes in ScrollPane.
		  By passing in JLabel to ScrollPane, if setText is changed, JLabels on 
		  Conferences tab will change accordingly.
		 */
		flag.addPropertyChangeListener(new PropertyChangeListener(){
			@Override
			public void propertyChange(PropertyChangeEvent e) {
				if (e.getSource() == flag) {
					Object[] data = scroll.getRow(scroll.getSelectedRowNum());

					for (Conference conf : controller.getConferenceList()) {
						for (Paper p : conf.getConferencePapers()) {
							if (p.getUniqueCode() == ((Paper) data[2]).getUniqueCode()) {

								//set text for deadline JLabel
								int rev_day = conf.getReviewsDeadline().getDateDay();
								int rev_month = conf.getReviewsDeadline().getDateMonth();
								int rev_year = conf.getReviewsDeadline().getDateYear();
								deadline.setText(rev_year + "/" + rev_month + "/" + rev_day);

								Calendar today = Calendar.getInstance();
								int cur_day = today.get(Calendar.DATE);
								int cur_mon = today.get(Calendar.MONTH);
								int cur_yr = today.get(Calendar.YEAR);
										
								if (cur_yr <= rev_year) {
									if (cur_mon <= rev_month) {
										if (cur_day <= rev_day) {
											review.setEnabled(true);
										}
										review.setEnabled(true);
									}
								} else {
									review.setEnabled(false);
								}
								
								//set text for status JLabel
								boolean find = false;
								for (Review r : p.getReviewList()) {
									if (controller.getCurrentUser().getfullname().equals(r.getReviewer().getfullname())) {
										status.setText("Reviewed");
										find = true;
										break;
									} 
								}
								if (!find) {
									status.setText("Not yet reviewed");
								}
							}
						}
					}
				}
			} //propertyChange
		});

		scroll.setFlag(flag);
		JScrollPane pane = scroll.getScrollPane();
		pane.setWheelScrollingEnabled(true);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(pane);
		add(status());
		add(deadline());
		add(btns());
	} //setup

	/**
	 * Sets up the look of the status labels.
	 * 
	 * @return Container containing the labels.
	 * @author Ching-Ting Huang
	 */
	private Container status() {
		Container flow = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel lblStatus = new JLabel("Status: ");
		flow.add(lblStatus);
		flow.add(status);
		return flow;
	} //status

	/**
	 * Sets up the look of the deadline labels.
	 * 
	 * @return Container containing the labels.
	 * @author Ching-Ting Huang
	 */
	private Container deadline() {
		Container flow = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel lblDeadline = new JLabel("Deadline");
		flow.add(lblDeadline);
		flow.add(deadline);
		return flow;
	} //deadline

	/**
	 * Sets up the button and listeners.
	 * 
	 * @return Container containing the buttons.
	 * @author Ching-Ting Huang
	 */
	private Container btns() {
		Container flow = new JPanel(new FlowLayout(FlowLayout.CENTER));
		final JButton read = new JButton("Read Manuscript");
		final JButton download = new JButton("Download Manuscript");
		
		read.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == read) {
					byte[] text = new byte[]{};

					if (scroll.getSelectedRowNum() != -1) {
						Object[] selected = scroll.getRow(scroll.getSelectedRowNum());
						Paper chosen = (Paper) selected[2];
						Paper paper = null;
						for (Conference conf : controller.getConferenceList()) {
							for (Paper p : conf.getConferencePapers()) {
								if (chosen.getUniqueCode() == p.getUniqueCode()) {
									paper = p;
								}
							}
						}
						ViewManuscript edit = new ViewManuscript((JFrame) SwingUtilities.windowForComponent(read),
								"Read Manuscript", paper);
						edit.start(text);

					} else if (scroll.getSelectedRowNum() == -1) {
						JOptionPane.showMessageDialog(null, "Please select a manuscript to read.",
								"Select Manuscript", JOptionPane.WARNING_MESSAGE);
					}
				}
			} //actionPerformed
		});

		download.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == download) {
					if (scroll.getSelectedRowNum() != -1) {
						Object[] selected = scroll.getRow(scroll.getSelectedRowNum());
						Paper chosen = (Paper)selected[2];
						Paper paper = null;
						for (Conference conf : controller.getConferenceList()) {
							for (Paper p : conf.getConferencePapers()) {
								if (chosen.getUniqueCode() == p.getUniqueCode()) {
									paper = p;
								}
							}
						}
						SaveManuscript save = new SaveManuscript((JFrame) SwingUtilities.windowForComponent(download),
								"Save Manuscript", paper);
						save.start();

					} else if (scroll.getSelectedRowNum() == -1) {
						JOptionPane.showMessageDialog(null, "Please select a manuscript to save.",
								"Save Manuscript", JOptionPane.WARNING_MESSAGE);
					}
				}
			} //actionPerformed
		});
		
		//Edited by Jonathan
		review.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == review) {
					//review score and comments stored here
					Object[] data = new Object[11];

					//if a paper is selected
					if (scroll.getSelectedRowNum() != -1) {
						Object[] selected = scroll.getRow(scroll.getSelectedRowNum());
						Paper chosen = (Paper)selected[2];
						Conference c = null;
						Paper paper = null;
						for (Conference conf : controller.getConferenceList()) {
							for (Paper p : conf.getConferencePapers()) {
								if (chosen.getUniqueCode() == p.getUniqueCode()) {
									paper = p;
									c = conf;
								}
							}
						}
						
						//checks if the review deadline for the paper has already passed.
						Deadline d = c.getReviewsDeadline();
						Calendar now = Calendar.getInstance();
						Calendar dl = Calendar.getInstance();
						dl.set(d.getDateYear(), d.getDateMonth() - 1, d.getDateDay());
						
						if (now.before(dl)) {
						
							//check if current user has already reviewed this paper
							Review r = null;
							int reviewIndex = -1;
							boolean same_reviewer = false;
							for (int i = 0; i < paper.getReviewList().size(); i++) {
								if (paper.getReviewList().get(i).getReviewer().getfullname().equals(controller.getCurrentUser().getfullname())) {
									same_reviewer = true;
									r = paper.getReviewList().get(i);
									reviewIndex = i;
								}
							}
							
							ReviewPaper rev = null;
							if (!same_reviewer) {
								rev = new ReviewPaper((JFrame) SwingUtilities.windowForComponent(review),
										"Review Manuscript", paper);
							} else if (same_reviewer){
								rev = new ReviewPaper((JFrame) SwingUtilities.windowForComponent(review),
										"Review Manuscript", paper, r);
							}
							rev.start(data);
							data = rev.getData();
							boolean flag = true;
							for (int i = 0; i < data.length; i++) {
								if (data[i] == null) {
									flag = false;
								}
							}
	
							if (flag) {
								rev.dispose();
								int q1 = (Integer) data[0];
								int q2 = (Integer) data[1];
								int q3 = (Integer) data[2];
								int q4 = (Integer) data[3];
								int q5 = (Integer) data[4];
								int q6 = (Integer) data[5];
								int q7 = (Integer) data[6];
								int q8 = (Integer) data[7];
								int q9 = (Integer) data[8];
								int q10 = (Integer) data[9];
								String comment = (String) data[10];
								
								if (!same_reviewer) {
									controller.reviewPaper(paper, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, comment, controller.getCurrentUser());
								} else {
									controller.replaceReview(paper, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, comment, controller.getCurrentUser(), reviewIndex);
								}
								status.setText("Reviewed");
							}
							
						} else {
							JOptionPane.showMessageDialog(null, "You can not review this manuscript. The review deadline has already passed.",
									"Overdue", JOptionPane.WARNING_MESSAGE);
						}
						//no paper is selected
					} else if (scroll.getSelectedRowNum() == -1) {
						JOptionPane.showMessageDialog(null, "Please select a manuscript to review.",
								"Review Manuscript", JOptionPane.ERROR_MESSAGE);
					}
				}
			} //actionPerformed
		});

		flow.add(read);
		flow.add(download);
		flow.add(review);
		return flow;
	} //btns

	/**
	 * Used to print out title for tabbed panes.
	 * 
	 * @author Ching-Ting Huang
	 */
	public String getName() {
		return "Peer-Review Papers";
	} //getName



	//==================== Inner class: Review =======================//

	/**
	 * This creates a JDialog containing peer-review functionality.
	 * 
	 * @author Ching-Ting Huang
	 * @version UWTCSS 360, Spring 2013. 3-squared-software-systems.
	 */
	private class ReviewPaper extends JDialog {

		/**
		 * 
		 */
		private static final long serialVersionUID = -76303092547731199L;
		private Paper paper;
		private Review review;
		private Object[] obj;
		//GUI for user input
		private List<ButtonGroup> group;
		private JTextArea comment;

		/**
		 * Initialize fields.
		 * 
		 * @param frame is the frame this JDialog will center on.
		 * @param title is the title of the JDialog.
		 * @param p is the paper currently selected.
		 */
		private ReviewPaper(JFrame frame, String title, Paper p) {
			this(frame, title, p, null);
		} //constructor

		private ReviewPaper(JFrame frame, String title, Paper p, Review r) {
			super(frame, title);
			paper = p;
			review = r;
			populateList();
			setup();
		}

		/**
		 * Populate the list of ButtonGroup based on number of questions
		 * in the review.
		 */
		private void populateList() {
			group = new ArrayList<ButtonGroup>();
			for (int i = 0; i < 10; i++) {
				group.add(new ButtonGroup());
			}
		} //populateList
		
		/**
		 * This sets up the look of the JDialog.
		 */
		private void setup() {
			JPanel main = new JPanel();
			JPanel reviews = new JPanel();
			JLabel title = new JLabel("Peer-review for " + paper.getTitle());
			Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
			main.setBorder(border);
			
			title.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 24));
			main.setLayout(new BorderLayout());
			main.setPreferredSize(new Dimension(600, 700));
			reviews.setLayout(new BoxLayout(reviews, BoxLayout.Y_AXIS));

			//containing different questions from review form
			JPanel one = q1();
			JPanel two = q2();
			JPanel three = q3();
			JPanel four = q4();
			JPanel five = q5();
			JPanel six = q6();
			JPanel seven = q7();
			JPanel eight = q8();
			JPanel nine = q9();
			JPanel ten = q10();
			JPanel comm = comments();
			Container btns = btns();

			reviews.add(one);
			reviews.add(two);
			reviews.add(three);
			reviews.add(four);
			reviews.add(five);
			reviews.add(six);
			reviews.add(seven);
			reviews.add(eight);
			reviews.add(nine);
			reviews.add(ten);
			reviews.add(comm);

			main.add(title, BorderLayout.NORTH);
			main.add(reviews, BorderLayout.CENTER);
			main.add(btns, BorderLayout.SOUTH);

			add(main);
			pack();
		} //setup

		/**
		 * Sets up the labels and radio button for question 1 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q1() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>1. Can the content be directly applied by classroom instructors or curriculum designers?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q1 = group.get(0);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q1.add(c1);
			q1.add(c2);
			q1.add(c3);
			q1.add(c4);
			q1.add(c5);
			
			if (review != null) {
				int choice = review.getDirectAndObvious();
				Enumeration<AbstractButton> btns = q1.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q1

		/**
		 * Sets up the labels and radio button for question 2 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q2() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>2. Does the work appeal to a broad readership interested in engineering education or is it narrowly specialized?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q2 = group.get(1);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q2.add(c1);
			q2.add(c2);
			q2.add(c3);
			q2.add(c4);
			q2.add(c5);

			if (review != null) {
				int choice = review.getBroadOrNarrow();
				Enumeration<AbstractButton> btns = q2.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q2

		/**
		 * Sets up the labels and radio button for question 3 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q3() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>3. Does the work address a significant problem?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q3 = group.get(2);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q3.add(c1);
			q3.add(c2);
			q3.add(c3);
			q3.add(c4);
			q3.add(c5);

			if (review != null) {
				int choice = review.getSignificantOrNot();
				Enumeration<AbstractButton> btns = q3.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q3

		/**
		 * Sets up the labels and radio button for question 4 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q4() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>4. Does the author build upon relevant references and bodies of knowledge?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q4 = group.get(3);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q4.add(c1);
			q4.add(c2);
			q4.add(c3);
			q4.add(c4);
			q4.add(c5);

			if (review != null) {
				int choice = review.getRelevantRef();
				Enumeration<AbstractButton> btns = q4.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q4

		/**
		 * Sets up the labels and radio button for question 5 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q5() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>5. If a teaching intervention is reported, is it adequately evaluated in terms of its impact on learning in actual use?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q5 = group.get(4);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q5.add(c1);
			q5.add(c2);
			q5.add(c3);
			q5.add(c4);
			q5.add(c5);

			if (review != null) {
				int choice = review.getTeachingIntervention();
				Enumeration<AbstractButton> btns = q5.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q5

		/**
		 * Sets up the labels and radio button for question 6 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q6() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>6. Does the author use methods appropriate to the goals, both for the instructional intervention and the evaluation of impact on learning?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q6 = group.get(5);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q6.add(c1);
			q6.add(c2);
			q6.add(c3);
			q6.add(c4);
			q6.add(c5);

			if (review != null) {
				int choice = review.getGoalMethods();
				Enumeration<AbstractButton> btns = q6.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q6

		/**
		 * Sets up the labels and radio button for question 7 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q7() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>7. Did the author provide sufficient detail to replicate and evaluate?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q7 = group.get(6);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q7.add(c1);
			q7.add(c2);
			q7.add(c3);
			q7.add(c4);
			q7.add(c5);

			if (review != null) {
				int choice = review.getReplicateEvaluate();
				Enumeration<AbstractButton> btns = q7.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q7

		/**
		 * Sets up the labels and radio button for question 8 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q8() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>8. Is the paper clearly and carefully written?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q8 = group.get(7);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q8.add(c1);
			q8.add(c2);
			q8.add(c3);
			q8.add(c4);
			q8.add(c5);

			if (review != null) {
				int choice = review.getWellWritten();
				Enumeration<AbstractButton> btns = q8.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q8

		/**
		 * Sets up the labels and radio button for question 9 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q9() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>9. Does the paper adhere to accepted standards of style, usage, and composition?<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q9 = group.get(8);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q9.add(c1);
			q9.add(c2);
			q9.add(c3);
			q9.add(c4);
			q9.add(c5);

			if (review != null) {
				int choice = review.getAcceptedStandards();
				Enumeration<AbstractButton> btns = q9.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q9

		/**
		 * Sets up the labels and radio button for question 10 of the review.
		 * 
		 * @return JPanel containing all components.
		 */
		private JPanel q10() {
			JPanel box = new JPanel();
			Container btn = new JPanel(new FlowLayout(FlowLayout.LEADING));
			box.setLayout(new BorderLayout());
			String q = "<html>10. Overall publish acceptance.<br></html>";
			JLabel lblQ = new JLabel(q);

			ButtonGroup q10 = group.get(9);
			JRadioButton c1 = new JRadioButton("5");
			JRadioButton c2 = new JRadioButton("4");
			JRadioButton c3 = new JRadioButton("3");
			JRadioButton c4 = new JRadioButton("2");
			JRadioButton c5 = new JRadioButton("1");

			q10.add(c1);
			q10.add(c2);
			q10.add(c3);
			q10.add(c4);
			q10.add(c5);

			if (review != null) {
				int choice = review.getSummaryRating();
				Enumeration<AbstractButton> btns = q10.getElements();  
				while(btns.hasMoreElements()) {  
					JRadioButton temp = (JRadioButton)btns.nextElement();
					if(Integer.parseInt(temp.getText()) == choice) {  
						temp.setSelected(true);
					}  
				}
			}
			
			btn.add(c1);
			btn.add(c2);
			btn.add(c3);
			btn.add(c4);
			btn.add(c5);

			box.add(lblQ, BorderLayout.NORTH);
			box.add(btn, BorderLayout.CENTER);
			return box;
		} //q10

		/**
		 * Sets up the JTextArea where the reviewer can type in extra comments
		 * to the author of the paper.
		 * 
		 * @return JPanel containing JScrollPane which contains JTextArea.
		 */
		private JPanel comments() {
			JPanel panel = new JPanel();
			Container lbl = new JPanel(new FlowLayout(FlowLayout.LEADING));
			JLabel lblComm = new JLabel("Other Comments:");
			panel.setLayout(new BorderLayout());
			panel.setPreferredSize(new Dimension(100, 100));
			
			if (review != null) {
				comment = new JTextArea(review.getComments());
			} else {
				comment = new JTextArea();
			}
			comment.setEditable(true);
			comment.setLineWrap(true);
			comment.setWrapStyleWord(true);
			JScrollPane text = new JScrollPane(comment);

			lbl.add(lblComm);
			panel.add(lbl, BorderLayout.NORTH);
			panel.add(text, BorderLayout.CENTER);
			return panel;
		} //comments

		/**
		 * Sets up JButtons' action listener and add them to the panel to be
		 * added to the main panel.
		 * 
		 * @return the container containing two buttons with flow layout.
		 */
		private Container btns() {
			Container flow = new JPanel(new FlowLayout(FlowLayout.CENTER));
			final JButton confirm = new JButton("Confirm");
			final JButton cancel = new JButton("Cancel");

			confirm.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == confirm) {

						update();
						//check if any fields are unfilled
						boolean check = true;
						for (int i = 0; i < group.size(); i++) {
							if ((Integer)obj[i] == -1) {
								check = false;
							}
						}
						if (((String) obj[obj.length - 1]).length() == 0) {
							check = false;
						}
						
						if (!check) {
							JOptionPane.showMessageDialog(null, "Please make sure all fields are filled in.",
									"Incomplete Form", JOptionPane.ERROR_MESSAGE);
							setVisible(true);
						} else {
							setVisible(false);
						}
					}
				}
			});

			cancel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == cancel) {
						dispose();
					}
				}
			});

			flow.add(confirm);
			flow.add(cancel);
			return flow;
		} //getButtons

		/**
		 * Gets the selected button out of a specified ButtonGroup.
		 * 
		 * @param grp is the ButtonGroup.
		 * @return parsed selected JRadioButton's text.
		 */
		private int getSelected(ButtonGroup grp) {
			String text = null;
			Enumeration<AbstractButton> btns = grp.getElements();  
			while(btns.hasMoreElements()) {  
				JRadioButton temp = (JRadioButton)btns.nextElement();  
				if(temp.isSelected()) {  
					text = temp.getText(); 
					break;
				}  
			} 
			
			if (text == null) {
				return -1;
			}
			return Integer.parseInt(text);
		} //getSelected

		/**
		 * Populates the object array with user's input.
		 */
		private void update() {
			for (int i = 0; i < group.size(); i++) {
				obj[i] = getSelected(group.get(i));
			}
			obj[10] = comment.getText();
		} //update

		/**
		 * Let other classes gets the updated, populated array of data.
		 * 
		 * @return the Object array with user input.
		 */
		public Object[] getData() {
			return obj;
		} //getData

		/**
		 * This JDialog will be displayed when this method is called.
		 * 
		 * @param getObj is the array that the user input will be stored.
		 */
		public void start(Object[] getObj) {
			obj = getObj;
			setModal(true);
			setLocationRelativeTo(null);
			setVisible(true);
		} //start
	} //Review

	
	//===================== Inner class: ViewManuscript =======================//
	
	/**
	 * This class is JDialog containing a text field filled with the selected paper's
	 * content as well as word count.
	 * 
	 * @author Ching-Ting Huang
	 * @version UWTCSS 360, Spring 2013. 3-squared-software-systems.
	 */
	private class ViewManuscript extends JDialog {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1869065425026869471L;
		private JLabel words;
		private JTextArea area;
		private Paper paper;
		private byte[] text;
		private String content;

		/**
		 * Initialize the fields.
		 * 
		 * @param frame is the frame this JDialog will center on.
		 * @param title is the title for the JDialog.
		 * @param p is the paper currently selected.
		 */
		private ViewManuscript(JFrame frame, String title, Paper p) {
			super(frame, title);
			paper = p;
			try {
				content = new String(paper.loadDigitalPaper(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			setup();
		} //Edit

		/**
		 * Sets up the basic layout of the JDialog and calling other methods to
		 * fulfill more functionality of their component.
		 */
		private void setup() {
			JPanel main = new JPanel();
			JPanel grid = new JPanel();
			JTextArea txt = displayContent();
			Container btns = buttons();
			JPanel labels = labels(btns);
			JLabel header = new JLabel("Read Manuscript");
			header.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 22));
			JScrollPane pane = new JScrollPane(txt);

			grid.setLayout(new BorderLayout());
			grid.add(pane, BorderLayout.CENTER);
			grid.add(labels, BorderLayout.EAST);

			Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
			main.setBorder(border);
			main.setPreferredSize(new Dimension(450, 400));
			main.setLayout(new BorderLayout());
			main.add(header, BorderLayout.NORTH);
			main.add(grid, BorderLayout.CENTER);

			add(main);
			pack();
		} //setup

		/**
		 * Sets up the labels for the JDialog.
		 * 
		 * @param buttons is the Container containing buttons.
		 * @return the container containing the top portion of the JDialog.
		 */
		private JPanel labels(Container buttons) {
			JPanel all = new JPanel();
			JPanel format = new JPanel();
			format.setLayout(new BorderLayout());
			Container info = new JPanel(new BorderLayout());
			all.setLayout(new BoxLayout(all, BoxLayout.Y_AXIS));
			
			Container titles = new JPanel(new FlowLayout(FlowLayout.LEADING));
			Container name = new JPanel(new FlowLayout(FlowLayout.LEADING));
			Container cate = new JPanel(new FlowLayout(FlowLayout.LEADING));
			Container wdc = new JPanel(new FlowLayout(FlowLayout.LEADING));

			JLabel lblCount = new JLabel("Word count: ");
			JLabel title = new JLabel("Title: " + paper.getTitle());
			JLabel author = new JLabel("Author: " + paper.getName());
			JLabel category = new JLabel("Category: " + paper.getCategory());
			words = new JLabel("" + count());

			titles.add(title);
			name.add(author);
			cate.add(category);
			wdc.add(lblCount);
			wdc.add(words);
			
			info.add(name, BorderLayout.NORTH);
			info.add(cate, BorderLayout.SOUTH);
			
			all.add(titles, BorderLayout.NORTH);
			all.add(info, BorderLayout.CENTER);
			all.add(wdc, BorderLayout.SOUTH);
			format.add(all, BorderLayout.NORTH);
			format.add(buttons, BorderLayout.SOUTH);
			return format;
		} //labels

		/**
		 * Set up JTextArea with paper content and provide functions for editing.
		 * 
		 * @return JTextArea of the paper.
		 */
		private JTextArea displayContent() {
			area = new JTextArea(content);
			area.setEditable(false);
			area.setLineWrap(true);
			area.setWrapStyleWord(true);

			area.getDocument().addDocumentListener(new DocumentListener() {
				@Override
				public void changedUpdate(DocumentEvent e) {
					words.setText("" + count());
				} //changedUpdate

				@Override
				public void insertUpdate(DocumentEvent e) {
					words.setText("" + count());
				} //insertUpdate

				@Override
				public void removeUpdate(DocumentEvent e) {
					words.setText("" + count());
				} //removeUpdate
			});
			return area;
		} //displayContent

		/**
		 * Sets up the buttons and their listeners and store them into FlowLayout
		 * Container to be displayed on the JDialog.
		 * 
		 * @return the Container containing the buttons.
		 */
		private Container buttons() {
			Container flow = new JPanel(new FlowLayout(FlowLayout.CENTER));
			final JButton close = new JButton("Close");

			close.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == close) {
						text = area.getText().getBytes();
						paper.saveDigitalPaper(text);
						dispose();
					}
				}
			});

			flow.add(close);
			return flow;
		} //buttons

		/**
		 * Counts the current words in the JTextArea.
		 * 
		 * @return the word count.
		 */
		private int count() {
			StringTokenizer st = new StringTokenizer(area.getText());
			return st.countTokens();
		} //count

		/**
		 * This is called from the Reviewers tab in order to display the JDialog
		 * and to capture results when closing.
		 * 
		 * @param txt is the Byte[] representation of the contents in the paper.
		 */
		public void start(byte[] txt) {
			text = txt;
			
			addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					int result = JOptionPane.showConfirmDialog(null, 
							"Do you want to close the window?", 
							"Closing", 
							JOptionPane.YES_NO_OPTION);
					
					if (result == JOptionPane.OK_OPTION) {
						ViewManuscript.this.dispose();
					}
				}
			});
			
			setModal(true);
			setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
			setLocationRelativeTo(null);
			setVisible(true);

		} //start
	} //ViewManuscript
	
	
	
	//================== Inner class: Save =========================//
	
	/**
	 * Gives user choices of where to save the file as well as what name they
	 * want to name the file.
	 * 
	 * @author Ching-Ting Huang
	 * @version UWTCSS 360, Spring 2013. 3-squared-software-systems.
	 */
	private class SaveManuscript extends JDialog {

		/**
		 * 
		 */
		private static final long serialVersionUID = 7987335874871014828L;
		private Paper paper;
		private JTextField fileName;
		private JTextField path;

		/**
		 * Initialize fields.
		 * 
		 * @param frame is the frame the JDialog will be centered on.
		 * @param title is the title of the JDialog.
		 * @param p is the currently selected paper.
		 */
		private SaveManuscript(JFrame frame, String title, Paper p) {
			super(frame, title);
			paper = p;
			setup();
		} //Save

		/**
		 * Sets up the looks of the JDialog.
		 */
		private void setup() {
			JPanel main = new JPanel();
			JPanel rest = new JPanel();
			Container name = fileName();
			Container dir = directory();
			Container btn = new JPanel(new FlowLayout(FlowLayout.CENTER));
			final JButton confirm = new JButton("Confirm");
			Border border = BorderFactory.createTitledBorder("Save Manuscript");
			main.setBorder(border);

			confirm.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == confirm) {
						String dir = path.getText();
						String fn = fileName.getText();

						if (dir.length() != 0 && fn.length() != 0) {
							//forcing the file to save as a .txt file
							if (!fn.substring(fn.length() - 5, fn.length() - 1).equals(".txt")) {
								fn = fn + ".txt";
							}
							String text = null;
							try {
								text = new String(paper.loadDigitalPaper(), "UTF-8");
							} catch (UnsupportedEncodingException e2) {
								e2.printStackTrace();
							}

							try {
								//write to file with user specified file name & path
								File file = new File(dir, fn);
								BufferedWriter bw = new BufferedWriter(new FileWriter(file));
								if (text != null) {
									StringTokenizer st = new StringTokenizer(text);
									while (st.hasMoreTokens()) {
										bw.write(st.nextToken());
										bw.write(" ");
									}
									bw.flush();
									bw.close();
								}
							} catch (IOException e1) {
								e1.printStackTrace();
							}
							JOptionPane.showMessageDialog(null, 
									"File successfully saved at: " + dir + " !", 
									"Closing", 
									JOptionPane.PLAIN_MESSAGE);
							SaveManuscript.this.dispose();
						} else if (fn.length() == 0) {
							JOptionPane.showMessageDialog(null, 
									"Please give the file a name.", 
									"File Name", 
									JOptionPane.ERROR_MESSAGE);
						} else if (dir.length() == 0) {
							JOptionPane.showMessageDialog(null, 
									"Please indicate a path for the file.", 
									"File Path", 
									JOptionPane.ERROR_MESSAGE);
						}
					}
				} //actionPerformed
			});

			btn.add(confirm);

			rest.setLayout(new BorderLayout());
			rest.add(name, BorderLayout.NORTH);
			rest.add(dir, BorderLayout.CENTER);

			main.setLayout(new BorderLayout());
			main.add(rest, BorderLayout.CENTER);
			main.add(btn, BorderLayout.SOUTH);
			add(main);
			pack();
		} //setup

		/**
		 * Sets up the looks for the file name input.
		 * 
		 * @return Container containing both label and text field.
		 */
		private Container fileName() {
			Container flow = new JPanel(new FlowLayout(FlowLayout.LEADING));
			JLabel lblName = new JLabel("File Name:");
			fileName = new JTextField("*.txt",20);
			fileName.setEditable(true);
			flow.add(lblName);
			flow.add(fileName);
			return flow;
		} //fileName

		/**
		 * Sets up the JTextField to pull up JFileChooser for the user to choose
		 * the path they wanted the file saved as well setting up the label.
		 * 
		 * @return Container containing the label and text field.
		 */
		private Container directory() {
			Container flow = new JPanel(new FlowLayout(FlowLayout.LEADING));
			JLabel lblDir = new JLabel("Directory: ");
			path = new JTextField(20);
			path.setEditable(false);

			path.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					int result = fc.showSaveDialog(Reviewer.this);
					if (result == JFileChooser.APPROVE_OPTION) {
						path.setText(fc.getSelectedFile().getAbsolutePath());
					}
				} //mouseClicked
			});

			flow.add(lblDir);
			flow.add(path);
			return flow;
		} //directory

		/**
		 * Sets up miscellaneous settings for the JDialog and make it visible
		 * when this method is called.
		 */
		public void start() {
			setModal(true);
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			setLocationRelativeTo(null);
			setVisible(true);
		} //start
	} //Save

} //outer
